package com.movisol.questionwizard.views;

import android.graphics.Color;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.movisol.adsservice.client.BannerViewer;
import com.movisol.questionwizard.activities.CustomActivity;
import com.movisol.questionwizard.interfaces.ScreenViewable;
import com.movisol.tools.HelperUtils;

public class Credits extends CustomActivity implements ScreenViewable,OnClickListener {
	private static final int CREDITS_BANNER_SLOT_ID =14;
	private static final int CREDITS_BANNER_SLOT_ID_AZ =19;
	private static final int CREDITS_BANNER_SLOT_ID_SS =24;
	Button btnBack;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);  
		context = ac.getContext();
		setContentView(R.layout.credits);
		ac.setExit(true);
		initializeControls();
		
		}
    
    
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(ac.isNeedAppToReboot())
		{
			exit();
		}
	}


	@Override
	public void initializeControls() {
		 
		//Initialize adBanner
		LinearLayout adLayout = (LinearLayout) findViewById(R.id.creditsPageAdLayout);
		adLayout.removeAllViews();
		if(ac.getSku().substring(ac.getSku().length()-2).equalsIgnoreCase("an"))
			bw = new BannerViewer(context, CREDITS_BANNER_SLOT_ID,this);
		if(ac.getSku().substring(ac.getSku().length()-2).equalsIgnoreCase("az"))
			bw = new BannerViewer(context, CREDITS_BANNER_SLOT_ID_AZ,this);
		if(ac.getSku().substring(ac.getSku().length()-2).equalsIgnoreCase("ss"))
			bw = new BannerViewer(context, CREDITS_BANNER_SLOT_ID_SS,this);
		
		adLayout.addView(bw);
			
		
		//Initializes the credits page background
		RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.creditsPageRealativeLayoutBackground);
		rLayout.setBackgroundResource(HelperUtils.getDrawableResource("backgroundcredits", getApplicationContext()));
		
		RelativeLayout tabLayout = (RelativeLayout) findViewById(R.id.tabLayout);
		tabLayout.setBackgroundResource(HelperUtils.getDrawableResource("tab", getApplicationContext()));
		
		
		//Initializes Previous Button that leads us to the Main Page
		btnBack=(Button)  findViewById(R.id.btnCreditsPageBack);
		btnBack.setText(ac.getLiteralsValueByKey("back"));
		btnBack.setBackgroundResource(HelperUtils.getDrawableResource("btn_back_credits", getApplicationContext()));
		btnBack.setTextAppearance(getApplicationContext(), HelperUtils.getStyleResource("btnBackCredits", getApplicationContext()));
		btnBack.setOnClickListener(this);
		
		//Builds the credits description appending the required html code to be able to read the CSS styles
		WebView webView=(WebView) findViewById(R.id.creditsPageWebLayout);
		webView.setBackgroundColor(Color.TRANSPARENT);
		StringBuffer sb = new StringBuffer();
		sb.append("<html><body><head>");
		sb.append(ac.getLiteralsValueByKey("styleCSS"));
		sb.append("</head>");
		sb.append("<div id=\"aboutBox\">");
		sb.append(ac.getLiteralsValueByKey("creditsWebBody"));
		sb.append(ac.getLiteralsValueByKey("moreAppsLocalHTML"));
		sb.append("</div>");
		sb.append("</body></html>");
		webView.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8",null);
		
		//Initializes the copyright footer
		TextView txtCopyRight= (TextView) findViewById(R.id.txtPageCreditsCopyRight);
		txtCopyRight.setText(ac.getLiteralsValueByKey("copyrightFooter"));		 
		
		//Loads the predefined style for the copyright footer
		txtCopyRight.setTextAppearance(getApplicationContext(), HelperUtils.getStyleResource("copyrightFooterStyle", getApplicationContext()));
	
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			ac.setExit(false);
			exit();
			return true;
		}
		else
			return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public void onClick(View v) {
		ac.setExit(false);
		exit();
	}

}
